package com.jzo2o.customer.service.impl;

import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mvc.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author Echo
 * @since 2025-03-07
 */
@Service
@RequiredArgsConstructor
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {
    private final IAgencyCertificationAuditService auditService;

    /**
     * 机构人员提交认证申请
     * @param dto
     */
    @Override
    @Transactional
    public void saveCertification(AgencyCertificationAuditAddReqDTO dto) {
        Long userId = UserContext.currentUserId();
        AgencyCertification certification = BeanUtils.copyBean(dto, AgencyCertification.class);
        certification.setId(userId);
        // 设置认证状态为认证中
        certification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        save(certification);
        dto.setServeProviderId(userId);
        // 保存机构人员认证信息到审核表
        auditService.saveCertification(dto);
    }

    /**
     * 查询驳回原因
     * @return
     */
    @Override
    public RejectReasonResDTO queryRejectedReason() {
        Long userId = UserContext.currentUserId();
        AgencyCertificationAudit audit = auditService.lambdaQuery()
                .eq(AgencyCertificationAudit::getServeProviderId, userId)
                .eq(AgencyCertificationAudit::getCertificationStatus, CertificationStatusEnum.FAIL.getStatus())
                .one();
        if (ObjectUtils.isNotEmpty(audit)) {
            return BeanUtils.copyBean(audit, RejectReasonResDTO.class);
        }
        return null;
    }
}
